﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace chemicall
{
    class Sprite
    {
        //The asset name for the Sprite's Texture
        public string AssetName;

        //The size of the Sprite
        public Rectangle spriteSize;

        //Used to size the Sprite up or down from the original image
        private int scale = 1;

        //The current position of the Sprite
        public Vector2 Position = new Vector2(0, 0);

        //the size of the entity displayed on screen
        public Rectangle entitySize;

        //The texture object used when drawing the sprite
        private Texture2D mSpriteTexture;
        Rectangle source;

        //Current underSpite in spritesheet
        Rectangle curSpritePosition;

        //frames info
        public int currentFrame = 0;
        private int nbFrame;
        private int nbAnimation;
        public int currAnim = 0;
        private float currTime = 0;

        protected Boolean flip = false;

        public void LoadContent(ContentManager theContentManager, string assetName, Nullable<Rectangle> entitySize, Nullable<Rectangle> spriteSize)
        {
            mSpriteTexture = theContentManager.Load<Texture2D>(assetName);
            AssetName = assetName;

            if (entitySize.HasValue)
                this.entitySize = entitySize.Value;
            else
                this.entitySize = new Rectangle(0,0,(int)(mSpriteTexture.Width * scale), (int)(mSpriteTexture.Height * scale));

            source = new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height);

            if (spriteSize.HasValue)
                this.spriteSize = spriteSize.Value;
            else
                this.spriteSize = new Rectangle(0, 0, this.entitySize.Width * scale, this.entitySize.Height * scale);

            curSpritePosition = new Rectangle(currentFrame * this.spriteSize.Width, currAnim * this.spriteSize.Height, this.spriteSize.Width, this.spriteSize.Height);

            nbFrame = mSpriteTexture.Width / this.spriteSize.Width;
            nbAnimation = mSpriteTexture.Height / this.spriteSize.Height;
        }

        //Draw the sprite to the screen
        public void Draw(SpriteBatch spriteBatch)
        {
            Vector2 pos = new Vector2(Position.X - this.entitySize.Width * 0.5f, Position.Y - this.entitySize.Height * 0.5f);
            spriteBatch.Draw(mSpriteTexture, pos, curSpritePosition, Color.White, 0.0f, Vector2.Zero, (float)scale, flip ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
        }

        public void Update(GameTime gameTime, Vector2 speed, Vector2 direction)
        {

            if (nbFrame > 1)
            {
                currTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (currTime >= 1 / 20.0f)
                {
                    currentFrame++;
                    if (currentFrame >= nbFrame)
                        currentFrame = 0;

                    curSpritePosition = new Rectangle(currentFrame * (int)this.spriteSize.Width, currAnim * this.spriteSize.Height, (int)this.spriteSize.Width, (int)this.spriteSize.Height);

                    currTime = 0;
                }
            }
            else
            {

                curSpritePosition = new Rectangle(currentFrame * (int)this.spriteSize.Width, currAnim * this.spriteSize.Height, (int)this.spriteSize.Width, (int)this.spriteSize.Height);
                currTime = 0;
            }
            Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        public Rectangle Source
        {
            get { return source; }
            set
            {
                source = value;
            }
        }
        public int Scale
        {

            get { return scale; }
            set
            {
                scale = value;
                entitySize = new Rectangle(entitySize.X,entitySize.Y,entitySize.Width*scale,entitySize.Height*scale);
            }
        }
    }
}
